<?php

Class Lib_CallCenter_YunKeFu_Main
{

    private $url = ''; //请求地址
    private $action = ''; //请求方法
    private $urlParam = ''; //url参数
    private $request_type = ''; //请求方式
    private $AccountId = ''; //身份标识
    private $APISecret = ''; //身份秘钥
    private $request_time = ''; //请求时间


    public function __construct($line = 1)
    {
        $env = getenv('RUNTIME_ENVIROMENT') ? getenv('RUNTIME_ENVIROMENT') : (defined('SHELL_VARIABLE') ? SHELL_VARIABLE : '');
        $env = empty($env) ? 'local' : $env;
        $config = require(LIBRARY_DIR . '/Conf/' . $env . '/callCenter.php');
        if (!isset($config)) {
            throw new Exception('can not found the callCenter env:' . $env . ' $config empty');
        }
        $this->url = $config['YunKeFu'][$line]['url'];
        $this->AccountId = $config['YunKeFu'][$line]['AccountId'];
        $this->APISecret = $config['YunKeFu'][$line]['APISecret'];
    }

    /**
     * 获取请求url参数
     */
    private function getHrader()
    {
        return ['Authorization:'.base64_encode($this->AccountId.':'.$this->request_time)];
    }

    /**
     * 获取请求签名
     */
    private function getSig()
    {
        return strtoupper(md5($this->AccountId.$this->APISecret.$this->request_time));
    }
   /*
     * post请求接口
     */
    public function Prequest($action, $params = [], $is_shell=0, $timeout = 20)
    {
        //初始化
        $this->request_time = date('YmdHis', time());
        $this->request_type = "POST";
        $this->action = $action;
        $header = $this->getHrader();
        $sig = $this->getSig();
        //初始化日志
        $AccountId =  $this->AccountId;
        //请求地址
        $url = strstr($action,'http') ? $action : $this->url.$action;        //初始化日志
        $url = $url."/{$AccountId}?sig={$sig}";
        if (!isset($params['ssid'])){
            $params['ssid']  = str_replace('.','',uniqid('',true));
        }

        $ssid =  $params['ssid'];
        $logs = 'url='.$url;
        $logs .= ' ssid='.$ssid;
        $logs .= ' header='.json_encode($header);
        $logs .= ' sig='.$sig;
        $logs .= ' request_time='.$this->request_time;
        //请求接口
        $result = Lib_CallCenter_YunKeFu_Curl::postjson($url, $params, $header, $timeout);
        //初始化返回值
        $res = array('code' => $result['errno'], 'message' => 'success', 'data' => array());
        $res['ssid'] = $ssid;
        unset( $params['ssid']);
        //连接错误
        if ($result['errno'] !== 200) {
            $message = Lib_CallCenter_YunKeFu_Notice::getCallMessage($action,$result['errno']);
            $res['data'] = $params;
            $res['message'] = empty($message) ? $result['message'] : $message;
            $logs .= ' request='.json_encode($params);
            $logs .= ' response='.json_encode($result);
            Func_Log::write('callCenter','WARNING',$logs.' link failure');return $res;
        }
        if(!empty($result['ActionID'])){
            $logs .= ' ActionID='.$result['ActionID'];
        }
        unset($result['errno']);
        if (isset($result['error'])){
            $res['code'] = 400;
            $res['message'] = $result['error']['message'];
            $logs .= ' request='.json_encode($params);
            $logs .= ' response='.json_encode($result);
            Func_Log::write('callCenter','WARNING',$logs.' return error');
        }
        unset($result['requestId']);
        Func_Log::write('callCenter','success',$logs.' request success');
        $res['data'] = $result['data'];
        return $res;
    }
}

